package com.example.xm.ui.home.fragments;

import android.content.Intent;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.RecommendAdapter;
import com.example.xm.adapter.HomeAdapter.TopicDiscusseAdapter;
import com.example.xm.base.BaseAdapter;
import com.example.xm.base.BaseFragment;
import com.example.xm.bean.HomeBean.HomeArticleBean;
import com.example.xm.bean.HomeBean.HomeBannderBean;
import com.example.xm.bean.HomeBean.HomeHotUserBean;
import com.example.xm.bean.HomeBean.HomePhotoBean;
import com.example.xm.bean.HomeBean.HomeRecommendBean;
import com.example.xm.bean.HomeBean.HomeRewardBean;
import com.example.xm.bean.HomeBean.HomeSquareBean;
import com.example.xm.bean.HomeBean.HomeTopicDiscussedBean;
import com.example.xm.bean.HomeBean.HomeVideoBean;
import com.example.xm.common.ConStants;
import com.example.xm.interfaces.home.IHome;
import com.example.xm.presenter.home.RecommendPresenter;
import com.example.xm.ui.home.activity.RecommendDetailActivity;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class Fragment_Recommend extends BaseFragment<IHome.RecommendPersenter> implements IHome.RecommendView {
    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.txt_discussed_title)
    TextView txtDiscussedTitle;
    @BindView(R.id.rlv_dicussed)
    RecyclerView mRlvDicussed;
    @BindView(R.id.recycler_commend)
    RecyclerView recyclerCommend;
    //热门话题
    private ArrayList<HomeTopicDiscussedBean.DataBean> topicdiscussed;
    private TopicDiscusseAdapter topicDiscusseAdapter;
    //推荐数据
    private ArrayList<HomeRecommendBean.DataBean.PostDetailBean> recommend;
    private RecommendAdapter recommendAdapter;
    //评论数据
    private ArrayList<HomeRecommendBean.DataBean.CommentsBean.AllCommentsBean> commentsBeans;
    //评论人数
    private int countnum = 0;

    @Override
    protected IHome.RecommendPersenter initPresenter() {
        return new RecommendPresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initView() {
        //设置热门话题——横向滑动
        setTopic();
        setRecommend();
        commentsBeans = new ArrayList<>();

    }

    private void setRecommend() {
        recommend = new ArrayList<>();
        recommendAdapter = new RecommendAdapter(getActivity(), recommend);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerCommend.setLayoutManager(manager);
        recyclerCommend.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
        recyclerCommend.setAdapter(recommendAdapter);
    }

    private void setTopic() {
        topicdiscussed = new ArrayList<>();
        topicDiscusseAdapter = new TopicDiscusseAdapter(getActivity(), topicdiscussed);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRlvDicussed.setLayoutManager(manager);
        mRlvDicussed.setAdapter(topicDiscusseAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getBanner();
        mPresenter.getHotUser();
        mPresenter.getRecommend();
        mPresenter.getTopicDiscussed();
    }

    @Override
    protected void initListener() {
        recommendAdapter.setOnClick(new BaseAdapter.onClick() {
            @Override
            public void click(int pos) {
                HomeRecommendBean.DataBean.PostDetailBean bean = recommend.get(pos);

                //activity页面之间传递数据的方式 1.static修饰的全局静态变量
                //2.把要传递的数据转换成为json结构的字符串，通过intent传递
                //3.Base64
                //4.Sp
                //5.数据库
                ConStants.curClickPost = bean;
                ConStants.pallComments = commentsBeans;
                ConStants.countNum = countnum;
                Intent intent = new Intent(getActivity(), RecommendDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    //banner数据
    @Override
    public void getBannerReturn(HomeBannderBean result) {
        if (result.getStatus().getStatusCode() == 100) {
            //初始化banner
            List<String> bannerUrls = new ArrayList<>();
            if (result.getData().getList().size() > 0) {
                for (int i = 0; i < result.getData().getList().size(); i++) {
                    bannerUrls.add(result.getData().getList().get(i).getBanner());
                    bannerUrls.add(result.getData().getList().get(i).getBanner());
                    bannerUrls.add(result.getData().getList().get(i).getBanner());
                    bannerUrls.add(result.getData().getList().get(i).getBanner());
                }
            }

            mBanner.setAdapter(new BannerImageAdapter<String>(bannerUrls) {
                @Override
                public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(data)
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(80)))//圆角弧度
                            .into(holder.imageView);
                }
            })
                    .addBannerLifecycleObserver(this)//添加生命周期观察者
                    .setIndicator(new CircleIndicator(getActivity()));
        }
    }

    @Override
    public void getTopicDiscussedReturn(HomeTopicDiscussedBean result) {
        if (result.getStatus().getStatusCode() == 100) {
            topicdiscussed.addAll(result.getData());
            topicDiscusseAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getRecommendReturn(HomeRecommendBean result) {
        if (result.getStatus().getStatusCode() == 100) {
            recommend.add(result.getData().getPostDetail());
            int countNumber = result.getData().getComments().getCountNumber();
            ArrayList<HomeRecommendBean.DataBean.CommentsBean.AllCommentsBean> allComments = (ArrayList<HomeRecommendBean.DataBean.CommentsBean.AllCommentsBean>) result.getData().getComments().getAllComments();
            countnum = countNumber;
            commentsBeans.addAll(allComments);
            recommendAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getHotUserReturn(HomeHotUserBean result) {

    }

    @Override
    public void getSquareReturn(HomeSquareBean result) {

    }

    @Override
    public void getVideoReturn(HomeVideoBean result) {

    }

    @Override
    public void getPhotoReturn(HomePhotoBean result) {

    }

    @Override
    public void getHomeArticleReturn(HomeArticleBean result) {

    }

    @Override
    public void getHomeRewardReturn(HomeRewardBean result) {

    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }
}
